package info.tsukaworld.actor.mob.pathfinding;

import info.tsukaworld.grid.*;

import java.util.HashSet;
import java.util.Set;

public abstract class Maze
{
    protected final ModifiableTile[][] tiles;

    public Maze(Grid gr)
    {
        int  height = gr.getNumRows();
        int width = gr.getNumCols();
        tiles = new ModifiableTile[height][width];

        for(int row = 0; row < height; row++)
        {
            for(int col = 0; col < width; col++)
            {
                final ModifiableTile tile;
                final Location       location;

                location        = new Location(row, col);
                tile            = new ModifiableTile(location);
                /*
                if( gr.get( new Location( row, col )) != null  && 
                !(gr.get( new Location( row, col )) instanceof Arrow))
                tile.setObstacle( new Obstacle() );*/
                if( gr.get( new Location( row, col )) != null)
                    tile.setObstacle( new Obstacle() );
                tiles[row][col] = tile;
            }
        }
    }

    public int getRows()
    {
        return (tiles.length);
    }

    public int getCols()
    {
        return (tiles[0].length);
    }

    public Tile getTileAt(final Location loc)
    {
        final Tile tile;

        tile = tiles[loc.getRow()][loc.getCol()];

        return (tile);
    }

    public Set<Tile> getTilesAround(final Location location)
    {
        final Set<Tile> nearTiles;
        final int       middleRow;
        final int       middleCol;

        nearTiles = new HashSet<Tile>();
        middleRow = location.getRow();
        middleCol = location.getCol();

        for(int row = middleRow - 1; row <= middleRow + 1; row++)
        {
            for(int col = middleCol - 1; col <= middleCol + 1; col++)
            {
                final Tile tile;

                if(row < 0 || col < 0)
                {
                    continue;
                }

                if(row >= tiles.length || col >= tiles[0].length)
                {
                    continue;
                }

                if(row == middleRow && col == middleCol)
                {
                    continue;
                }
                if(row - middleRow == 0 || col - middleCol == 0)
                {
                    tile = tiles[row][col];
                    nearTiles.add(tile);
                }
            }
        }

        return (nearTiles);
    }
}